package com.jebel.framework.gateway.handler.impl;

import com.jebel.framework.gateway.handler.PublicServiceFilterHandler;
import com.jebel.framwork.common.response.RespBodyBuilder;
import com.jebel.framwork.common.response.RespCode;
import com.jebel.framwork.common.util.JsonUtil;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PublicServiceFilterHandlerImpl implements PublicServiceFilterHandler {

private static Logger logger = LoggerFactory.getLogger ( PublicServiceFilterHandlerImpl.class );
private Vertx vertx;

public PublicServiceFilterHandlerImpl(Vertx vertx) {
	this.vertx = vertx;
}

@Override
public void handle(RoutingContext ctx) {
	HttpServerResponse response = ctx.response ();
	response.putHeader ( "content-type" , "application/json;charset=UTF-8" );
	response.putHeader ( "Access-Control-Allow-Origin" , "*" );
	response.putHeader ( "Access-Control-Allow-Methods" , "POST,GET" );
	response.putHeader ( "Access-Control-Allow-Headers" , "x-requested-with,content-type" );
	String method = ctx.request ().method ().name ();
	JsonObject body = method.equals ( "POST" ) ? ctx.getBodyAsJson () : JsonUtil.convertMultiMap ( ctx.request ().params () );
	String service = body.getString ( "service" );
	
	if (service.startsWith ( "user" ) || service.startsWith ( "login" ) || service.startsWith ( "network" ) || service.startsWith ( "agent" )) {
		ctx.next ();
	} else {
		response.end ( RespBodyBuilder.toError ( RespCode.CODE_10500 ) );
		return;
	}
}
}
